package com.xci.sys.api;

import com.xci.core.annotation.Authorize;
import com.xci.core.base.ApiController;
import com.xci.core.base.PrimaryKeyValue;
import com.xci.core.base.RestMessage;
import com.xci.core.helper.ExcelHelper;
import com.xci.core.internal.Const;
import com.xci.sys.entity.SysModule;
import com.xci.sys.filter.ModuleFilter;
import com.xci.sys.service.ModuleService;
import io.swagger.annotations.*;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 模块接口
 * @author 吕艳阳
 */
@ApiSort(6)
@Api(tags = "模块接口")
@RestController
@Authorize
@RequestMapping(value = "/api/sys/module", produces = Const.P_JSON)
public class ModuleApiController extends ApiController {
    @Resource
    private ModuleService moduleService;//模块服务

    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "新建模块")
    @Authorize(code = "sys.module.insert")
    @PostMapping("/insert")
    public RestMessage insert(@RequestBody SysModule entity) {
        return moduleService.insert(entity);
    }

    @ApiOperationSupport(order = 2)
    @ApiOperation(value = "修改模块")
    @Authorize(code = "sys.module.update")
    @PostMapping("/update")
    public RestMessage update(@RequestBody SysModule entity) {
        return moduleService.update(entity);
    }

    @ApiOperationSupport(order = 3)
    @ApiOperation(value = "修改模块状态")
    @ApiImplicitParams({@ApiImplicitParam(name = "ids", value = "模块主键字符串", required = true), @ApiImplicitParam(name = "status", value = "模块状态", required = true)})
    @Authorize(code = "sys.module.updateStatus")
    @PostMapping("/updateStatus")
    public RestMessage updateStatus(String ids, Boolean status) {
        return moduleService.updateStatus(ids, status);
    }

    @ApiOperationSupport(order = 4)
    @ApiOperation(value = "修改模块公共状态")
    @ApiImplicitParams({@ApiImplicitParam(name = "ids", value = "模块主键字符串", required = true),
                               @ApiImplicitParam(name = "publicStatus", value = "模块公共状态", required = true)})
    @PostMapping("/updatePublicStatus")
    public RestMessage updatePublicStatus(String ids, Boolean publicStatus) {
        return moduleService.updatePublicStatus(ids, publicStatus);
    }

    @ApiOperationSupport(order = 5)
    @ApiOperation(value = "修改模块展开状态")
    @ApiImplicitParams({@ApiImplicitParam(name = "ids", value = "模块主键字符串", required = true),
                               @ApiImplicitParam(name = "expandStatus", value = "模块展开状态", required = true)})
    @PostMapping("/updateExpandStatus")
    public RestMessage updateExpandStatus(String ids, Boolean expandStatus) {
        return moduleService.updateExpandStatus(ids, expandStatus);
    }

    @ApiOperationSupport(order = 6)
    @ApiOperation(value = "修改模块预加载状态")
    @ApiImplicitParams({@ApiImplicitParam(name = "ids", value = "模块主键字符串", required = true),
                               @ApiImplicitParam(name = "status", value = "模块预加载状态", required = true)})
    @PostMapping("/updatePreloadingStatus")
    public RestMessage updatePreloadingStatus(String ids, Boolean preloadingStatus) {
        return moduleService.updatePreloadingStatus(ids, preloadingStatus);
    }

    /**
     * 批量修改字段值
     * @param keyValues 字段值集合
     */
    @ApiOperationSupport(order = 7)
    @ApiOperation(value = "批量修改字段值")
    @PostMapping("/batchUpdate")
    public RestMessage batchUpdate(@RequestBody List<PrimaryKeyValue> keyValues) {
        return moduleService.batchUpdate(keyValues);
    }

    @ApiOperationSupport(order = 8)
    @ApiOperation(value = "删除模块")
    @ApiImplicitParam(name = "ids", value = "模块主键字符串", required = true)
    @Authorize(code = "sys.module.delete")
    @PostMapping("/delete")
    public RestMessage delete(String ids) {
        return moduleService.delete(ids);
    }

    @ApiOperationSupport(order = 9)
    @ApiOperation(value = "检查模块主键是否存在")
    @ApiImplicitParam(name = "id", value = "模块主键", required = true)
    @PostMapping("/existById/{id}")
    public RestMessage existById(@PathVariable String id) {
        return RestMessage.result(moduleService.existById(id));
    }

    @ApiOperationSupport(order = 10)
    @ApiOperation(value = "检查模块编码是否存在")
    @ApiImplicitParam(name = "code", value = "模块编码", required = true)
    @PostMapping("/existByCode/{code}")
    public RestMessage existByCode(@PathVariable String code) {
        return RestMessage.result(moduleService.existByCode(code));
    }

    @ApiOperationSupport(order = 11)
    @ApiOperation(value = "查询单个模块")
    @ApiImplicitParam(name = "id", value = "模块主键", required = true)
    @PostMapping("/selectById/{id}")
    public RestMessage<SysModule> selectById(@PathVariable String id) {
        return RestMessage.success(moduleService.selectById(id));
    }

    @ApiOperationSupport(order = 12)
    @ApiOperation(value = "根据编码查询单个模块")
    @ApiImplicitParam(name = "code", value = "模块编码", required = true)
    @PostMapping("/selectByCode/{code}")
    public RestMessage<SysModule> selectByCode(@PathVariable String code) {
        return RestMessage.success(moduleService.selectByCode(code));
    }

    @ApiOperationSupport(order = 13)
    @ApiOperation(value = "获取节点的直系子节点数")
    @ApiImplicitParam(name = "id", value = "机构主键", required = true)
    @PostMapping("/selectChildCount/{id}")
    public RestMessage selectChildCount(@PathVariable String id) {
        return RestMessage.success(moduleService.selectChildCount(id));
    }

    @ApiOperationSupport(order = 14)
    @ApiOperation(value = "查询模块列表")
    @PostMapping("/selectList")
    public RestMessage<List<SysModule>> selectList(ModuleFilter filter) {
        return RestMessage.success(moduleService.selectList(filter));
    }

    @ApiOperationSupport(order = 15)
    @ApiOperation(value = "导出模块列表")
    @ApiImplicitParam(name = "fieldNames", value = "导出字段名字符串")
    @Authorize(code = "sys.module.export")
    @PostMapping(value = "/export", produces = {Const.P_OCTET, Const.P_JSON})
    public void export(ModuleFilter filter, @RequestParam(required = false) String fieldNames) {
        ExcelHelper.exportWeb(moduleService.selectList(filter), SysModule.class, "系统模块列表");
    }

    @ApiOperationSupport(order = 16)
    @ApiOperation(value = "获取参数导出字段")
    @PostMapping(value = "/exportNames")
    public RestMessage<List<String>> exportNames() {
        return RestMessage.success(ExcelHelper.getExcelFiledNames(SysModule.class));
    }

    @ApiOperationSupport(order = 17)
    @ApiOperation(value = "根据用户主键查询用户拥有的模块列表")
    @ApiImplicitParam(name = "userId", value = "用户主键", required = true)
    @PostMapping("/selectUserModuleList/{userId}")
    public RestMessage<List<SysModule>> selectUserModuleList(@PathVariable String userId) {
        return RestMessage.success(moduleService.selectUserModuleList(getUser(userId)));
    }

    @ApiOperationSupport(order = 18)
    @ApiOperation(value = "根据用户主键查询用户拥有的模块缓存列表")
    @ApiImplicitParam(name = "userId", value = "用户主键", required = true)
    @PostMapping("/getUserModuleCacheList/{userId}")
    public RestMessage<List<SysModule>> getUserModuleCacheList(@PathVariable String userId) {
        return RestMessage.success(moduleService.getUserModuleCacheList(getUser(userId)));
    }

    @ApiOperationSupport(order = 19)
    @ApiOperation(value = "清除用户拥有模块缓存")
    @ApiImplicitParam(name = "userId", value = "用户主键", required = true)
    @PostMapping("/clearUserModuleCache/{userId}")
    public RestMessage clearUserModuleCache(@PathVariable String userId) {
        moduleService.clearUserModuleCache(userId);
        return RestMessage.success();
    }
}